<?php
getDB();
class moneyData extends DBData
{
    public $tablename;
    public function __construct()
    {
        $this->tablename = "x_user_money";
        DBData::$tableName = 'x_user_money';
        bcscale(2);
    }
    public function getInfo($uid)
    {
        $arrInfo = $this->getRecord("*", "uid='$uid'", "", $this->tablename);
        if(!$arrInfo)
        {
            $arrInfo = array('uid'=>$uid,'money'=>0,'cash_money'=>0,'withdraw_money'=>0,'freeze_money'=>0,'consume_money'=>0,'chongzhi_money'=>0,'other_money'=>0,'jifen_money'=>0);
            $this->inserttable($arrInfo, 0, $this->tablename);
        }
         
        return $arrInfo;
    }
    public function _getmoney($uid,$fields)
    {
        $arrInfo = $this->getRecord($fields, "uid='$uid'", "", $this->tablename);
        $val = $arrInfo[$fields];
        return $val;
    }
    //余额直接消费
    public function consumemoney($moneyInfo,$money)
    {
        // $_money = (float)$moneyInfo['money'];
        // $_consume_money = (float)$moneyInfo['consume_money'];
        // $moneyInfo['money'] = $_money - $money;
        // $moneyInfo['consume_money'] = $_consume_money + $money;

        $moneyInfo['money'] = bcsub($moneyInfo['money'],$money) ;
        $moneyInfo['consume_money'] = bcadd($moneyInfo['consume_money'],$money) ;

        return $moneyInfo;
    }
    //直接支付消费
    public function consume($moneyInfo,$money)
    {
        // $_consume_money = (float)$moneyInfo['consume_money'];
        // $moneyInfo['consume_money'] = $_consume_money + $money;
        $moneyInfo['consume_money'] = bcadd($moneyInfo['consume_money'],$money);
        return $moneyInfo;
    }
    
    //统计到其他的费用:比如牛金卷
    public function other($moneyInfo,$money)
    {
        // $_money = (float)$moneyInfo['other_money'];
        // $moneyInfo['other_money'] = $_money + $money;
        $moneyInfo['other_money'] = bcadd($moneyInfo['other_money'],$money);
        return $moneyInfo;
    }
    //消费到其他的金额里
    public function transother($moneyInfo,$money)
    {
        // $_money = (float)$moneyInfo['money'];
        // $moneyInfo['money'] = $_money - $money;
        // $moneyInfo['other_money'] = (float)$moneyInfo['other_money'] + $money;
        $moneyInfo['money'] = bcsub($moneyInfo['money'],$money);
        $moneyInfo['other_money'] - bcadd($moneyInfo['other_money'],$money);
        return $moneyInfo;
    }
    //余额的钱到现金余额里
    public function transcash($moneyInfo,$money,$cash_money=0)
    {
        $_money = (float)$moneyInfo['money'];
        $_cash = (float)$moneyInfo['cash_money']; 
        // $moneyInfo['money'] = $_money - $money;
        $moneyInfo['money'] = bcsub($moneyInfo['money'],$money);
        if($cash_money == 0)
        {
            $cash_money = $money;
        }
        // $moneyInfo['cash_money'] = $_cash + $cash_money;
        $moneyInfo['cash_money'] = bcadd($moneyInfo['cash_money'],$cash_money);
        return $moneyInfo;
    }
    
    //增加现金红包
    public function addcashmoney($moneyInfo,$cash)
    {
        // $_cash = (float)$moneyInfo['cash_money'];
        // $moneyInfo['cash_money'] = $_cash + $cash;
        $moneyInfo['cash_money'] = bcadd($moneyInfo['cash_money'],$cash);
        return $moneyInfo;
    }
    
    //充值
    public function chongzhimoney($moneyInfo,$money)
    {
        // $_chongzhi_money = $moneyInfo['chongzhi_money'];
        // $_money = $moneyInfo['money'];
        // $moneyInfo['money'] = $_money + $money;
        // $moneyInfo['chongzhi_money'] = $_chongzhi_money + $money;

        $moneyInfo['money'] = bcadd($moneyInfo['money'],$money);
        $moneyInfo['chongzhi_money'] = bcadd($moneyInfo['chongzhi_money'],$money);
        return $moneyInfo;
    }
    public function chongzhicashmoney($moneyInfo,$money)
    {
        // $_chongzhi_money = $moneyInfo['chongzhi_money'];
        // $_money = $moneyInfo['cash_money'];
        // $moneyInfo['cash_money'] = $_money + $money;
        // $moneyInfo['chongzhi_money'] = $_chongzhi_money + $money;

        $moneyInfo['cash_money'] = bcadd($moneyInfo['cash_money'],$money);
        $moneyInfo['chongzhi_money'] = bcadd($moneyInfo['chongzhi_money'],$money);
        return $moneyInfo;
    }
    //积分返现的钱
    public function addjifenmoney($moneyInfo,$money)
    {
        // $_jifen_money = $moneyInfo['jifen_money'];
        // $_money = $moneyInfo['money'];
        // $moneyInfo['money'] = $_money + $money;
        // $moneyInfo['jifen_money'] = $_jifen_money + $money;
        
        $moneyInfo['money'] = bcadd($moneyInfo['money'],$money);
        $moneyInfo['jifen_money'] = bcadd($moneyInfo['jifen_money'],$money);
        return $moneyInfo;
    }
    //冻结金额：提现，或者预定
    public function freezemoney($moneyInfo,$money)
    {
        // $_money = (float)$moneyInfo['money'];
        // $_change_money = (float)$moneyInfo['freeze_money'];
        // $moneyInfo['money'] = $_money - $money;
        // $moneyInfo['freeze_money'] = $_change_money + $money;

        $moneyInfo['money'] = bcsub($moneyInfo['money'],$money);
        $moneyInfo['freeze_money'] = bcadd($moneyInfo['freeze_money'],$money);
        return $moneyInfo;
    }
    public function freezecash($moneyInfo,$money)
    {
        // $_money = (float)$moneyInfo['cash_money'];
        // $_change_money = (float)$moneyInfo['freeze_money'];
        // $moneyInfo['cash_money'] = $_money - $money;
        // $moneyInfo['freeze_money'] = $_change_money + $money;
        
        $moneyInfo['cash_money'] = bcsub($moneyInfo['cash_money'],$money);
        $moneyInfo['freeze_money'] = bcadd($moneyInfo['freeze_money'],$money);
        return $moneyInfo;
    }
    //从冻结里，转移到消费里
    public function freeze_to_consume($moneyInfo,$money)
    {
        // $_money = (float)$moneyInfo['consume_money'];
        // $_change_money = (float)$moneyInfo['freeze_money'];
        // $moneyInfo['consume_money'] = $_money + $money;
        // $moneyInfo['freeze_money'] = $_change_money - $money;

        $moneyInfo['consume_money'] = bcadd($moneyInfo['consume_money'],$money);
        $moneyInfo['freeze_money'] = bcsub($moneyInfo['freeze_money'],$money);
        return $moneyInfo;
    }
    //现金余额的变动
    public function editfreeze($moneyInfo,$money)
    {
        // $_change_money = (float)$moneyInfo['freeze_money']; 
        // $moneyInfo['freeze_money'] = $_change_money + $money;

        $moneyInfo['freeze_money'] = bcadd($moneyInfo['freeze_money'],$money);
        return $moneyInfo;
    }
    //获取余额
    public function balance($moneyInfo)
    {
        // $money = (float)$moneyInfo['money'];
        // $cash_money = (float)$moneyInfo['cash_money']; 
        // $balance = $money + $cash_money;
        $balance = bcadd($moneyInfo['money'],$moneyInfo['cash_money']);
        return (float)$balance;
    }
    //减少活跃金的时候，不够从现金越里抵扣：主要是购买自由转换的牛金卷：用这个的函数的时候，先提前判断下是否余额够抵扣
    public function cutmoney($moneyInfo,$cutmoney)
    {
        $money = $moneyInfo['money']; 
        if(bccomp($moneyInfo['money'],$cutmoney) >= 0)
        {
            $moneyInfo['money'] = bcsub($moneyInfo['money'],$cutmoney);
        }else
        {
            // $cha = (float)$cutmoney - $money;
            // $cash_money = (float)$moneyInfo['cash_money']; 
            // $moneyInfo['money'] = 0;
            // $moneyInfo['cash_money'] = $cash_money - $cha;

            $cha = bcsub($cutmoney,$money);
            $moneyInfo['money'] = 0;
            $moneyInfo['cash_money'] = bcsub($moneyInfo['cash_money'],$cha);
        }
        return $moneyInfo;
    }
    //提现成功
    public function withdrawmoney($moneyInfo,$money,$freeze_money=0)
    {
        // $_money = (float)$moneyInfo['withdraw_money'];
        // $_change_money = (float)$moneyInfo['freeze_money'];
        if($freeze_money == 0)
        {
            $freeze_money = $money;
        }
        // $moneyInfo['withdraw_money'] = $_money + $money;
        // $moneyInfo['freeze_money'] = $_change_money - $freeze_money;
        
        $moneyInfo['withdraw_money'] = bcadd($moneyInfo['withdraw_money'],$money);
        $moneyInfo['freeze_money'] = bcsub($moneyInfo['freeze_money'],$freeze_money);
        return $moneyInfo;
    }
    //提现失败：把冻结金额返回到余额里
    public function withdrawFail($moneyInfo,$withdraw)
    {
        // $freeze_money = $moneyInfo['freeze_money'];
        // $money = $moneyInfo['money'];
        // $moneyInfo['money'] = $money + $withdraw;
        // $moneyInfo['freeze_money'] = $freeze_money - $withdraw;

        $moneyInfo['money'] = bcadd($moneyInfo['money'],$withdraw);
        $moneyInfo['freeze_money'] = bcsub($moneyInfo['freeze_money'],$withdraw);
        return $moneyInfo;
    }
    //提现失败：把冻结金额返回到余额里
    public function cashwithdrawFail($moneyInfo,$withdraw)
    {
        // $freeze_money = $moneyInfo['freeze_money'];
        // $money = $moneyInfo['cash_money'];
        // $moneyInfo['cash_money'] = $money + $withdraw;
        // $moneyInfo['freeze_money'] = $freeze_money - $withdraw;

        $moneyInfo['cash_money'] = bcadd($moneyInfo['cash_money'],$withdraw);
        $moneyInfo['freeze_money'] = bcsub($moneyInfo['freeze_money'],$withdraw);
        return $moneyInfo;
    }
    
    public function  edit_money($moneyInfo,$money)
    {
        // $_money = $moneyInfo['money'];
        // $moneyInfo['money'] = $_money + ($money);

        $moneyInfo['money'] = bcadd($moneyInfo['money'],$money);
        return $moneyInfo;
    }
    public function edit_limit($moneyInfo,$money)
    {
        // $_money = $moneyInfo['limit_withdraw'];
        // $moneyInfo['limit_withdraw'] = $_money + ($money);

        $moneyInfo['limit_withdraw'] = bcadd($moneyInfo['limit_withdraw'],$money);
        return $moneyInfo;
    }
    
    //当冻结金额小于0的时候触发的事情
    public function handle()
    {
        
    }
    public function _up($moneyInfo)
    {
        $uid = $moneyInfo['uid'];
        unset($moneyInfo['uid']);
        $this->uptable($moneyInfo, "uid='$uid'", $this->tablename);
    }
    public function up($moneyInfo,$userArr=array())
    {
        $uid = $moneyInfo['uid'];
        unset($moneyInfo['uid']);
        $upnum = $this->uptable($moneyInfo, "uid='$uid'", $this->tablename);
        if(!is_numeric($upnum))
        {
            return array('status'=>'err','msg'=>'更新失败');
        }
        $money = $moneyInfo['money']; 
        $cash_money = $moneyInfo['cash_money'];
        $up_data = array('cash_money'=>$cash_money,'money'=>$money);
        if($userArr)
        {
            $up_data = array_merge($up_data, $userArr);
        }
        $upnum = $this->uptable($up_data, "id='$uid'", "web_user"); 
        if(!is_numeric($upnum))
        {
            return array('status'=>'err','msg'=>'更新失败。');
        }
        return array('status'=>'ok','msg'=>'ok','upnum'=>$upnum);
    }
    
}
?>